#ifndef _IHTMLTYLEREADER_H_
#define _IHTMLTYLEREADER_H_

#include <BMap>
#include <BTable>
#include <BIcon>
#include <BImage>
#include <BString>
#include <BWidget>
#include "IHtmlStyle.h"
#include "IFontFamily.h"

namespace BWE
{
	bool StringToColor(const BString& text, BColor& color);
	bool StringToAlign(const BString& text, Align& align);
	bool StringToForm(BString& text, Form& form);
	bool StringToPoint(BString& text, BPoint& point);
	bool StringToSize(BString& text, BSize& size);
	bool StringToRect(const BString& text, BRect& rect);
	bool StringToMargin(const BString& text, BMargin& margin);
	bool StringToBool(BString& text, bool& b);

	BHtmlItem::Display TextToDisplay(const BString& text);
	BHtmlItem::Style TextToStyle(const BString& text);

	class IHtmlStyleReader
	{
	public:
		State StringToState(const char* cstr);
		State StringToState(const BString& str);
		bool SplitImage(const BImage* image, BTable<BImageHolder>& imageTable);

	public:
		IHtmlStyleReader();
		~IHtmlStyleReader();

		bool setPath(const BString& path);
		const BString& path() const;

		IHtmlStyle* load(const BString& fileName);
		bool load(const BString& fileName, IHtmlStyle* style);

		IHtmlStyle* read(const BString& text);
		bool read(const BString& text, IHtmlStyle* style);
		void read(IHtmlStyle* style, const BString& text, int& pos);

		const BString& message() const;

	protected:
		bool readComment(const BString& text, int& pos, BString& comment);
		IHtmlStyle* findChild(BString& text, IHtmlStyle* style);
		IHtmlStyle* addStyle(BString& text, IHtmlStyle* parent);
		IHtmlStyle* addSubStyle(BString& text, int pos, IHtmlStyle* style);
		void readDesc(BString& text, IHtmlStyle* style);
		bool analysis(BString& sign, BString& value, IHtmlStyle* style);

		bool analysisTitle(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisText(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisBackground(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisForeground(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisWidth(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisHeight(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisBorder(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisMargin(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisPadding(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisTransition(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisFont(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisList(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisGroove(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisGraph(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisIcon(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisImage(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisArrow(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisHandle(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisSeparator(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisGridLine(IHtmlStyle* style, BString& sign, BString& value);
		bool analysisFlex(IHtmlStyle* style, BString& sign, BString& value);

		bool locateUrl(BString& url);
		BImage* readImage(BString& fileName);
		BIcon* readIcon(BString& fileName);
		BString findFamily(BString& str);

		inline bool borderInfos(const BString& text, int& width, BColor& color, BString& style)
		{
			BStringArray words;
			text.split(words, ' ');
			for (int i = 0; i < words.size(); i++)
			{
				BString& word = words[i];
				if (word == "solid" || word == "dotted" || word == "double" || word == "dashed")
					style = word;
				else if (word.isNumber())
					width = word.toInt();
				else
				{
					if (!StringToColor(word, color))
					{
						_message << "\tError[" << _lineNumber + 1 << "] : " << "border-xxx : invalid color desc." << '\n';
						return false;
					}
				}
			}
			return true;
		}

	private:
		BString						_path;
		int							_lineNumber;
		int							_linePos;
		mutable BString				_message;
		BMap<BString, BImageHolder>	_imageMap;
		BMap<BString, BIconHolder>	_iconMap;
	};
}

#endif